package com.newenergy.wisehome.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.MyEvent;
import com.newenergy.wisehome.common.helper.SPUtil;
import com.newenergy.wisehome.common.helper.ToastUtil;
import com.newenergy.wisehome.common.util.NetWorkUtil;
import com.newenergy.wisehome.config.Constants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BDService extends Service {
    public static final String TAG = "LFX";

    private BDSpeechManager mBDSpeechManager;


    public BDService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
        mBDSpeechManager = new BDSpeechManager(this);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("LFX", "BDService onDestroy" );
        endSpeech();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "BDService onStartCommand: " );
        if (intent != null ){
            String cmd = intent.getStringExtra("service_cmd");
            if (!TextUtils.isEmpty(cmd)){
                if (cmd.equals("startSpeech")){
                    boolean isFirstTime = SPUtil.getBool(Constants.ISFIRST_TIMEBD, true);
                    if (isFirstTime) {
                        if (NetWorkUtil.isNetworkAvailable(this)) {
                            startSpeech();
                            SPUtil.put(Constants.ISFIRST_TIMEBD, false);
                        }else {
                            ToastUtil.toast(getString(R.string.connect_before_use));//"首次使用需要联网"
                        }
                    }else {
                        startSpeech();
                    }
                }
            }
        }

        return super.onStartCommand(intent, flags, startId);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleEvent(MyEvent event) {
        String tag = event.getTag();
        Object data = event.getData();
        switch (tag) {
            case Constants.STATUS_LANGURE:
                break;
            case Constants.NetWork.ENABLE_NET:
                if ((boolean) data){
                    boolean speechEnable = SPUtil.getBool(Constants.SPEECH_ENABLE, false);
                    if (speechEnable){
                        boolean isFirstTime = SPUtil.getBool(Constants.ISFIRST_TIMEBD, true);
                        if (isFirstTime){
                            startSpeech();
                            SPUtil.put(Constants.ISFIRST_TIMEBD, false);
                        }
                    }
                }
                break;

            case Constants.SPEECH_CMD://1启动识别，0启动唤醒
                Log.i(TAG, "BDService handleEvent:1启动识别，0启动唤醒 ");
                int resId = (int)data;
                mBDSpeechManager.type = 0;
                mBDSpeechManager.playMusic(resId);
                break;
        }
    }


    public void startSpeech(){
        if (mBDSpeechManager != null) {
            mBDSpeechManager.startBDSpeech();
        }
    }


    public void endSpeech(){
        if (mBDSpeechManager != null) {
            mBDSpeechManager.endBDSpeech();
        }
    }

}
